using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;

using Microsoft.MSE.Catalog.Module;
using Microsoft.MSE.Catalog.Services;
namespace Microsoft.MSE.Catalog.ServiceHost
{
    public partial class CatalogService : ServiceBase
    {
        private EventLogTraceListener m_eventLogListener;

        public CatalogService()
        {
            this.ServiceName = SC.ComponentName;
            TraceHelper.InitializeTraceSource("mseTraceSrc");

            if (!EventLog.SourceExists(SC.ComponentName))
            {
                EventLog.CreateEventSource(SC.ComponentName, SC.EventLogName);
            }
            m_eventLogListener = new EventLogTraceListener(SC.ComponentName);
            TraceHelper.MseTraceSource.Listeners.Add(m_eventLogListener);

            this.ServiceName = SC.ComponentName;
        }

        protected override void OnStart(string[] args)
        {
            try
            {
                TraceHelper.TraceInformation("Starting " + SC.ComponentName + "...");

                WCFCatalogSvcHost.StartServiceCompleted += delegate(object sender, StartServiceCompletedEventArgs e)
                {
                    if (e.Error == null) return;

                    TraceHelper.TraceError("Attempt to start " + SC.ComponentName + " failed: " + e.Error.ToString());
                    this.Stop();
                };

                WCFCatalogSvcHost.StartServiceAsync();
            }
            catch (Exception ex)
            {
                TraceHelper.TraceError("Attempt to start " + SC.ComponentName + " failed: " + ex.ToString());
                throw;
            }
        }

        protected override void OnStop()
        {
            try
            {
                WCFCatalogSvcHost.StopService();
            }
            catch (Exception ex)
            {
                TraceHelper.TraceError("Attempt to stop " + SC.ComponentName + " failed: " + ex.ToString());
                throw;
            }
        }
    }
}
